/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UpdateUserPanel.java
 *
 * Created on 18.Kas.2009, 20:47:44
 */

package fairhelper.panels;

import fairhelper.User;
import fairhelper.UserManager;
import fairhelper.language.Language;
import fairhelper.theme.Theme;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Feoran
 */
public class UpdateUserPanel extends javax.swing.JPanel {

    private User user;

    /** Creates new form UpdateUserPanel */
    public UpdateUserPanel(User user) {
        this.user = user;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        updatePanelFirstNameField = new javax.swing.JTextField();
        updatePanelSurnameField = new javax.swing.JTextField();
        updatePanelUserRoleField = new javax.swing.JComboBox();
        updatePanelOldPasswordField = new javax.swing.JPasswordField();
        updatePanelNewPasswordField = new javax.swing.JPasswordField();
        updatePanelNewPasswordAgainField = new javax.swing.JPasswordField();
        saveChangesButton = new javax.swing.JButton();
        cleanButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        setBackground(Theme.getColor("backgroundColor2"));
        setPreferredSize(new java.awt.Dimension(620, 480));

        jLabel1.setForeground(Theme.getColor("labelColor1"));
        jLabel1.setText(Language.getText("userNameLabel"));

        jLabel2.setForeground(Theme.getColor("labelColor1"));
        jLabel2.setText(Language.getText("firstNameField"));

        jLabel3.setForeground(Theme.getColor("labelColor1"));
        jLabel3.setText(Language.getText("surnameField"));

        jLabel4.setForeground(Theme.getColor("labelColor1"));
        jLabel4.setText(Language.getText("oldPasswordLabel"));

        jLabel5.setForeground(Theme.getColor("labelColor1"));
        jLabel5.setText(Language.getText("newPasswordLabel"));

        jLabel6.setForeground(Theme.getColor("labelColor1"));
        jLabel6.setText(Language.getText("newPasswordAgainLabel"));

        jLabel7.setForeground(Theme.getColor("labelColor1"));
        jLabel7.setText(Language.getText("roleLabel"));

        updatePanelFirstNameField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        updatePanelFirstNameField.setForeground(Theme.getColor("fieldForegroundColor1"));
        updatePanelFirstNameField.setText(user.getFirstName());

        updatePanelSurnameField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        updatePanelSurnameField.setForeground(Theme.getColor("fieldForegroundColor1"));
        updatePanelSurnameField.setText(user.getSurname());

        updatePanelUserRoleField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        updatePanelUserRoleField.setForeground(Theme.getColor("fieldForegroundColor1"));
        updatePanelUserRoleField.setModel(new javax.swing.DefaultComboBoxModel(getUserPriviledgesNameArray()));
        updatePanelUserRoleField.setSelectedIndex(getUserPriviledgeIndex());

        updatePanelOldPasswordField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        updatePanelOldPasswordField.setForeground(Theme.getColor("fieldForegroundColor1"));

        updatePanelNewPasswordField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        updatePanelNewPasswordField.setForeground(Theme.getColor("fieldForegroundColor1"));

        updatePanelNewPasswordAgainField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        updatePanelNewPasswordAgainField.setForeground(Theme.getColor("fieldForegroundColor1"));

        saveChangesButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        saveChangesButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        saveChangesButton.setText(Language.getText("saveChangesButton"));
        saveChangesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveChangesButtonActionPerformed(evt);
            }
        });

        cleanButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        cleanButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        cleanButton.setText(Language.getText("returnToDefaultButton"));
        cleanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanButtonActionPerformed(evt);
            }
        });

        backButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        backButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        backButton.setText(Language.getText("backButton"));

        jLabel8.setForeground(Theme.getColor("labelColor1"));
        jLabel8.setText(user.getUserName());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(148, 148, 148)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel7)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(saveChangesButton))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cleanButton)
                        .addGap(18, 18, 18)
                        .addComponent(backButton))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(updatePanelNewPasswordAgainField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(updatePanelNewPasswordField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(updatePanelOldPasswordField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(updatePanelUserRoleField, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updatePanelSurnameField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(updatePanelFirstNameField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)))
                .addContainerGap(188, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(updatePanelFirstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(updatePanelSurnameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(updatePanelUserRoleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(updatePanelOldPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(updatePanelNewPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(updatePanelNewPasswordAgainField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveChangesButton)
                    .addComponent(cleanButton)
                    .addComponent(backButton))
                .addContainerGap(88, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private String[] getUserPriviledgesNameArray() {
        String[] privilegesNameArray = UserManager.getPrivilegesNameArray(true);
        for (int i=0; i<privilegesNameArray.length; i++) {
            privilegesNameArray[i] = Language.getText("roleName" + privilegesNameArray[i]);
        }
        return privilegesNameArray;
    }

    public int getUserPriviledgeIndex() {
        String[] privilegesNameArray = UserManager.getPrivilegesNameArray(true);
        for (int i=0; i<privilegesNameArray.length; i++) {
            if(privilegesNameArray[i].equals(UserManager.getPrivileges(user.getPrivilegesId()).getPrivilegeName()))
                return i;
        }
        return -1;
    }

    private void cleanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanButtonActionPerformed
        updatePanelFirstNameField.setText(user.getFirstName());
        updatePanelSurnameField.setText(user.getSurname());
        updatePanelUserRoleField.setSelectedIndex(getUserPriviledgeIndex());
        updatePanelOldPasswordField.setText("");
        updatePanelNewPasswordField.setText("");
        updatePanelNewPasswordAgainField.setText("");
    }//GEN-LAST:event_cleanButtonActionPerformed

    private String oldUserName;
    private String newUserName;
    private String newFirstName;
    private String newLastName;
    private int role;
    private String oldPassword;
    private String newPassword;
    private String newPasswordAgain;
    private String message;

    private void saveChangesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveChangesButtonActionPerformed
            oldUserName = user.getUserName();
            newUserName = oldUserName;
            newFirstName = updatePanelFirstNameField.getText();
            newLastName = updatePanelSurnameField.getText();
            role = UserManager.getPrivileges(UserManager.getPrivilegesNameArray(true)[updatePanelUserRoleField.getSelectedIndex()]).getId();
            oldPassword = new String(updatePanelOldPasswordField.getPassword());
            newPassword = new String(updatePanelNewPasswordField.getPassword());
            newPasswordAgain = new String(updatePanelNewPasswordAgainField.getPassword());

            if (newUserName.equals(""))
                message = Language.getText("enterUserNameMessage");
            else if (!UserManager.userNameLegal(newUserName))
                message = Language.getText("usernameWrongCharsMessage");
            else if (!UserManager.userNameUnique(newUserName) && !newUserName.equals(user.getUserName()))
                message = newUserName + " " + Language.getText("userAlreadyExistsMessage");
            else {
                if (oldPassword.equals("") && newPassword.equals("") && newPasswordAgain.equals("")) {
                    updateExceptPassword();
                    message = Language.getText("userUpdatedMessage");
                } else {

                    if (oldPassword.equals(""))
                        message = Language.getText("enterOldPasswordMessage");
                    else if (newPassword.equals(""))
                        message = Language.getText("enterNewPasswordMessage");
                    else if (newPasswordAgain.equals(""))
                        message = Language.getText("enterNewPasswordAgainMessage");
                    else if (!newPassword.equals(newPasswordAgain))
                        message = Language.getText("newPasswordsNotSameMessage");
                    else if (!UserManager.passwordLegal(newPassword))
                        message = Language.getText("passwordWrongCharsMessage");
                    else if (!oldPassword.equals(UserManager.decodePassword(user.getPassword(), user.getUserName())))
                        message = Language.getText("oldPasswordWrondMeaage");
                    else {
                        updateWithPassword();
                        message = Language.getText("userUpdatedMessage");
                    }
                }
            }

            JOptionPane.showMessageDialog(null, message);
            updatePanelOldPasswordField.setText("");
            updatePanelNewPasswordField.setText("");
            updatePanelNewPasswordAgainField.setText("");


    }//GEN-LAST:event_saveChangesButtonActionPerformed

    /** Şifre değiştirilmediyse eski şifre aynen kaydedilir.
         */
        private void updateExceptPassword() {
            String password = UserManager.decodePassword(user.getPassword(), user.getUserName());
            User updatedUser = new User(newUserName, password, newFirstName, newLastName, role);
            UserManager.updateUser(oldUserName, updatedUser);
        }

        /** Şifre değiştirildiyse yeni şifre dahil edilerek kaydedilir.
         */
        private void updateWithPassword() {
            User updatedUser = new User(newUserName, newPassword, newFirstName, newLastName, role);
            UserManager.updateUser(oldUserName, updatedUser);
        }

    public JButton getBackButton() {
        return backButton;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JButton cleanButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JButton saveChangesButton;
    private javax.swing.JTextField updatePanelFirstNameField;
    private javax.swing.JPasswordField updatePanelNewPasswordAgainField;
    private javax.swing.JPasswordField updatePanelNewPasswordField;
    private javax.swing.JPasswordField updatePanelOldPasswordField;
    private javax.swing.JTextField updatePanelSurnameField;
    private javax.swing.JComboBox updatePanelUserRoleField;
    // End of variables declaration//GEN-END:variables

}
